package com.lee.springCloud.controller;

import com.lee.springCloud.domain.Payment;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import com.lee.springCloud.domain.CommonResult;

import javax.annotation.Resource;

/**
 * @author Lee
 * @createDate 2021/12/22 17:00
 */
@Controller
public class OrderController {
    public static final String PAYMENT_URL = "http://cloud-payment-service";

    /** http调用模板 **/
    @Resource
    private RestTemplate restTemplate;

    @PostMapping("/consumer/payment/create")
    @ResponseBody
    public CommonResult<Payment> create(@RequestBody Payment payment){
        return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class);
    }


    /**
     * 使用getForEntity方法访问服务提供者
     * @param id
     * @return
     */
    @GetMapping("/consumer/payment/getForEntity/{id}")
    @ResponseBody
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id){
        ResponseEntity<CommonResult> entity = restTemplate.getForEntity(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
        if(entity.getStatusCode().is2xxSuccessful()){
            return entity.getBody();
        }
        return entity.getBody();
    }
}
